<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.0.2">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/all.min.css">
  
  <link rel="stylesheet" href="/lib/animate-css/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"jiapengcai.gitee.io","root":"/","scheme":"Gemini","version":"8.0.0-rc.5","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":true,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":"valine","storage":true,"lazyload":false,"nav":null,"activeClass":"valine"},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false};
  </script>

  <meta name="description" content="定义“测试驱动开发”（Test-Driven-Development, 简称TDD）是一种编程风格，它包含了三个不断循环交织的部分：编码，测试（即单元测试），设计（即重构）。">
<meta property="og:type" content="article">
<meta property="og:title" content="测试驱动开发">
<meta property="og:url" content="https://jiapengcai.gitee.io/posts/16756/index.html">
<meta property="og:site_name" content="PenG">
<meta property="og:description" content="定义“测试驱动开发”（Test-Driven-Development, 简称TDD）是一种编程风格，它包含了三个不断循环交织的部分：编码，测试（即单元测试），设计（即重构）。">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-08-12T13:03:23.000Z">
<meta property="article:modified_time" content="2020-08-12T13:24:46.988Z">
<meta property="article:author" content="Jiapeng Cai">
<meta property="article:tag" content="翻译">
<meta property="article:tag" content="编程">
<meta property="article:tag" content="测试">
<meta property="article:tag" content="软件质量">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://jiapengcai.gitee.io/posts/16756/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>测试驱动开发 | PenG</title>
  






  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <main class="main">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader">
        <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">PenG</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
  </ul>
</nav>




</div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <section class="post-toc-wrap sidebar-panel">
          <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%AE%9A%E4%B9%89"><span class="nav-number">1.</span> <span class="nav-text">定义</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%A5%BD%E5%A4%84"><span class="nav-number">2.</span> <span class="nav-text">好处</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%B8%B8%E8%A7%81%E7%9A%84%E9%99%B7%E9%98%B1"><span class="nav-number">3.</span> <span class="nav-text">常见的陷阱</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B8%E5%9E%8B%E7%9A%84%E4%B8%AA%E4%BA%BA%E5%AE%B9%E6%98%93%E7%8A%AF%E7%9A%84%E9%94%99%E8%AF%AF%E5%8C%85%E6%8B%AC%EF%BC%9A"><span class="nav-number">3.1.</span> <span class="nav-text">典型的个人容易犯的错误包括：</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%85%B8%E5%9E%8B%E7%9A%84%E5%9B%A2%E9%98%9F%E5%AE%B9%E6%98%93%E8%B8%A9%E7%9A%84%E9%99%B7%E9%98%B1%E5%8C%85%E6%8B%AC%EF%BC%9A"><span class="nav-number">3.2.</span> <span class="nav-text">典型的团队容易踩的陷阱包括：</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BD%BF%E7%94%A8%E7%9A%84%E6%A0%87%E5%BF%97"><span class="nav-number">4.</span> <span class="nav-text">使用的标志</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%8A%80%E8%83%BD%E7%BA%A7%E5%88%AB"><span class="nav-number">5.</span> <span class="nav-text">技能级别</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%9D%E7%BA%A7"><span class="nav-number">5.1.</span> <span class="nav-text">初级</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%AD%E7%BA%A7"><span class="nav-number">5.2.</span> <span class="nav-text">中级</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%AB%98%E7%BA%A7"><span class="nav-number">5.3.</span> <span class="nav-text">高级</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%BC%95%E7%94%A8"><span class="nav-number">6.</span> <span class="nav-text">引用</span></a></li></ol></div>
      </section>
      <!--/noindex-->

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jiapeng Cai"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">Jiapeng Cai</p>
  <div class="site-description" itemprop="description">Life can be programmed.</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives">
          <span class="site-state-item-count">9</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">7</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <a href="https://github.com/jiapengcai" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;jiapengcai" rel="noopener" target="_blank"><i class="fab fa-github fa-fw"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:635793@gmail.com" title="E-Mail → mailto:635793@gmail.com" rel="noopener" target="_blank"><i class="fa fa-envelope fa-fw"></i>E-Mail</a>
      </span>
  </div>



      </section>
    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </header>

      
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <a href="https://github.com/jiapengcai" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

<noscript>
  <div id="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


      <div class="main-inner">
        

        <div class="content post posts-expand">
          

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://jiapengcai.gitee.io/posts/16756/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="Jiapeng Cai">
      <meta itemprop="description" content="Life can be programmed.">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="PenG">
    </span>

    
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          测试驱动开发
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2020-08-12 21:03:23 / 修改时间：21:24:46" itemprop="dateCreated datePublished" datetime="2020-08-12T21:03:23+08:00">2020-08-12</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E8%BD%AF%E4%BB%B6%E8%B4%A8%E9%87%8F/" itemprop="url" rel="index"><span itemprop="name">软件质量</span></a>
                </span>
            </span>

          
            <span id="/posts/16756/" class="post-meta-item leancloud_visitors" data-flag-title="测试驱动开发" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/posts/16756/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/posts/16756/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="定义"><a href="#定义" class="headerlink" title="定义"></a>定义</h1><p>“测试驱动开发”（Test-Driven-Development, 简称TDD）是一种编程风格，它包含了三个不断循环交织的部分：编码，测试（即单元测试），设计（即重构）。 </p>
<a id="more"></a> 
<p>它可以通过以下几条规则来进行描述：  </p>
<ul>
<li>编写一条单元测试，而且这条测试仅描述程序的一个特性；</li>
<li>运行这条测试。因为此时程序还没有这个特性，测试将不通过。</li>
<li>编写足够的代码，越简单越好，让测试通过。</li>
<li>重构代码，直到代码达到简洁的标准。</li>
<li>重复以上步骤，不断累积单元测试。</li>
</ul>
<h1 id="好处"><a href="#好处" class="headerlink" title="好处"></a>好处</h1><ul>
<li>很多团队反馈说刚开始开发时工作量会有所增加，但换来的是他们的缺陷率都显著降低了。</li>
<li>这些团队还反馈道，一开始增加的工作量基本上能使得项目后期的工作量减少。</li>
<li>很多资深人士都反馈道，采用TDD能提高代码设计质量，例如，高内聚和低耦合。虽然到目前为止没有什么研究表明上述观点。</li>
</ul>
<h1 id="常见的陷阱"><a href="#常见的陷阱" class="headerlink" title="常见的陷阱"></a>常见的陷阱</h1><h2 id="典型的个人容易犯的错误包括："><a href="#典型的个人容易犯的错误包括：" class="headerlink" title="典型的个人容易犯的错误包括："></a>典型的个人容易犯的错误包括：</h2><ul>
<li>忘了时时运行测试代码。</li>
<li>一次性写了太多的测试代码。</li>
<li>写出来的测试代码太长或者粒度太粗。</li>
<li>为琐碎的代码编写测试，如，对象访问器（译者注：什么是访问器，比如Java Bean的getter）。  <h2 id="典型的团队容易踩的陷阱包括："><a href="#典型的团队容易踩的陷阱包括：" class="headerlink" title="典型的团队容易踩的陷阱包括："></a>典型的团队容易踩的陷阱包括：</h2></li>
<li>部分采用，也就是团队里只有少数几个研发人员采用了TDD。</li>
<li>测试套件维护得不好，这通常会导致测试代码运行得太久。</li>
<li>废弃的测试套件（例如，很少运行或者从不运行），这可能是维护不当造成的，或者可能是团队更替造成的。</li>
</ul>
<h1 id="使用的标志"><a href="#使用的标志" class="headerlink" title="使用的标志"></a>使用的标志</h1><ul>
<li>“代码覆盖率”是证明使用TDD的常见方式。虽然高覆盖率无法保证TDD的正确使用，但如果覆盖率低于80%，那么很可能说明团队对TDD的掌握还不够。</li>
<li>每次签入（check in）生产代码时，版本控制日志应该能显示出签入的测试代码在数量上大体与签入的生产代码相同。</li>
</ul>
<h1 id="技能级别"><a href="#技能级别" class="headerlink" title="技能级别"></a>技能级别</h1><h2 id="初级"><a href="#初级" class="headerlink" title="初级"></a>初级</h2><ul>
<li>能够在编写代码前先写单元测试。</li>
<li>能够写足够的代码让单元测试跑通。<h2 id="中级"><a href="#中级" class="headerlink" title="中级"></a>中级</h2></li>
<li>能够实践“测试驱动bug修复”：在发现缺陷时，先编写能暴露这个缺陷的测试代码，然后再去编写修复代码。</li>
<li>能够将一段包含了复杂特性的程序分解成一系列的单元测试。</li>
<li>知道并且能够列举一些编写测试代码的策略（例如，在测试一个递归算法时，会先为递归结束的场景编写测试代码）。</li>
<li>能够从现有的单元测试里去除可复用的部分，为特定场景编写测试工具。<h2 id="高级"><a href="#高级" class="headerlink" title="高级"></a>高级</h2></li>
<li>能站在宏观角度为单元测试计划制定“路线图”，并在必要时对其进行修改。</li>
<li>能够“测试驱动”若干设计范式：面向对象，函数编程，事件驱动等。</li>
<li>能够“测试驱动”若干技术领域：计算，用户界面，持久数据访问等。</li>
</ul>
<h1 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h1><ul>
<li>[1] <a target="_blank" rel="noopener" href="https://www.agilealliance.org/glossary/tdd/">原文出处：https://www.agilealliance.org/glossary/tdd/</a></li>
</ul>

    </div>

    
    
    
        

<div>
<ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者： </strong>Jiapeng Cai
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://jiapengcai.gitee.io/posts/16756/" title="测试驱动开发">https://jiapengcai.gitee.io/posts/16756/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" rel="noopener" target="_blank"><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</a> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E7%BF%BB%E8%AF%91/" rel="tag"><i class="fa fa-tag"></i> 翻译</a>
              <a href="/tags/%E7%BC%96%E7%A8%8B/" rel="tag"><i class="fa fa-tag"></i> 编程</a>
              <a href="/tags/%E6%B5%8B%E8%AF%95/" rel="tag"><i class="fa fa-tag"></i> 测试</a>
              <a href="/tags/%E8%BD%AF%E4%BB%B6%E8%B4%A8%E9%87%8F/" rel="tag"><i class="fa fa-tag"></i> 软件质量</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/posts/24843/" rel="prev" title="单元测试">
      <i class="fa fa-chevron-left"></i> 单元测试
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



        </div>
        
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fab fa-affiliatetheme"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Jiapeng Cai</span>
</div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/next-boot.js"></script>

<script src="/js/bookmark.js"></script>


  















  

  

  


<script>
NexT.utils.loadComments('#valine-comments', () => {
  NexT.utils.getScript('//cdn.jsdelivr.net/npm/valine@1/dist/Valine.min.js', () => {
    new Valine(Object.assign({
      el  : '#valine-comments',
      path: location.pathname,
    }, {"enable":true,"appId":"9R584ubklhG3KtIebsybj2Fg-gzGzoHsz","appKey":"hTQwNb5tpJrtED29EvBeAshK","placeholder":"Just go go","avatar":"mm","meta":["nick","mail","link"],"pageSize":10,"language":"zh-cn","visitor":true,"comment_count":true,"recordIP":false,"serverURLs":null,"enableQQ":false,"requiredFields":[]}
    ));
  }, window.Valine);
});
</script>

</body>
</html>
